package com.cy.pj.sys.service.aspect;

import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.service.SysUserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SysAuthorityAspect {

    @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredAuthority)")
    void checkAuthority(){}

    @Around("checkAuthority()")
    public Object doBefore(ProceedingJoinPoint pj) throws Throwable {
        try {
            login();
            Object result = pj.proceed();
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return new Throwable("此处是个什么异常呢");
        }
    }

    @Autowired
    private SysUserService sysUserService;

    private void login() throws Throwable {
        // 客户端输入用户名和密码
        String username = "admin11223";
        String password = "c4c33035c5d8e840616c128db9f87b25";
        // 查找数据库
        SysUser sysUser = sysUserService.checkLogin(username);
        if (sysUser == null) throw new Throwable("用户名不存在");
        if (!password.equals(sysUser.getPassword())){
            throw new Throwable("密码错误");
        }
    }
}
